package com.isesol.seckill.rest;

import cn.apiclub.captcha.servlet.*;
import com.isesol.arch.common.uitil.redis.*;
import com.isesol.seckill.*;
import com.isesol.seckill.result.*;
import com.isesol.seckill.service.*;
import com.isesol.seckill.service.impl.*;
import org.slf4j.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.*;
import javax.servlet.http.*;

/**
 * 验证码
 *
 * @author Peter Zhang
 */
@RestController
@EnableAutoConfiguration
public class CaptchaController {

    protected Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Resource
    RedisUtil redisUtil;

    /**
     * 秒杀逻辑 Service
     */
    @Resource
    SecKillService secKillService;

    /**
     * 图片验证码 Service
     */
    @Resource
    CaptchaService captchaService;

    @Resource
    CountService countService;

    @RequestMapping("/seckill/visit")
    @ResponseBody
    public Result<CaptchaResult> visit(@RequestParam(value = "code", required = true) String code,
                      @RequestParam(value = "id", required = true) String id) throws Exception {

        try {

            String key = code + ":" + id;

            return countService.incCaptchaIdCount(key);

        } catch (Exception e) {

            LOGGER.error("【seckill/get】 error,code={},id={}", code, id, e);

        }

        return null;

    }

    @RequestMapping("/seckill/getCaptcha")
    @ResponseBody
    public void get(@RequestParam(value = "code", required = true) String code,
                    @RequestParam(value = "id", required = true) String id,
                    @RequestParam(value = "captchaId", required = true) String captchaId,
                    HttpServletResponse resp) throws Exception {

        try {

            CaptchaServletUtil.writeImage(resp, captchaService.getCaptcha(code, id, captchaId));

            resp.flushBuffer();

        } catch (Exception e) {

            LOGGER.error("【seckill/get】 error,code={},id={}", code, id, e);

        }

    }

    @RequestMapping("/seckill/kill")
    @ResponseBody
    Result answer(@RequestParam(value = "code", required = true) String code,
                  @RequestParam(value = "id", required = true) String id,
                  @RequestParam(value = "captchaId", required = true) String captchaId,
                  @RequestParam(value = "answer", required = true) String answer,
                  @RequestParam(value = "token", required = true) String token) throws Exception {

        LOGGER.debug("【seckill/answer】调用," +
                "code={},id={},captchaId={},answer={},token={}",
                new Object[]{code, id, captchaId, answer, token});

        Result result;

        try {

            result = secKillService.secKill(code, id, captchaId, answer, token);

        } catch (Exception e) {

            result = new Result<SecKillResult>();
            result.setSuccess(false);
            result.setErrorMessage(e.getMessage());

        }

        LOGGER.debug("【seckill/answer】完成," +
                        " code={},id={},captchaId={},answer={},token={}",
                new Object[]{code, id, captchaId, answer, token});

        return result;
    }
}
